from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import re


# ***********************************************************************************************************************************************
#   注意！本脚本是用selenium进行自动化挂科，学习通不允许打开两个章节学习页面
#   所以，执行本脚本后不要在自己打开章节学习页面！
class XXT():

    # 进入视频章节学习页面，打开开发者工具，查看表单各项值，输入：
    chapterId = int(input('请输入 chapterid:'))
    courseId = int(input('请输入 courseId:'))
    clazzid = int(input('请输入 clazzid:'))
    cpi = int(input('请输入 cpi:'))
    enc = input('请输入 enc:')
    mooc2 = int(input('请输入 mooc2:'))
    openc = input('请输入 openc:')



    def __init__(self):
        # 登录
        self.drive = webdriver.Edge()
        self.drive.get(
            f"https://mooc1.chaoxing.com/mycourse/studentstudy?chapterId={XXT.chapterId}&courseId={XXT.courseId}&clazzid={XXT.clazzid}&cpi={XXT.cpi}&enc={XXT.enc}&mooc2={XXT.mooc2}&openc={XXT.mooc2}")
        time.sleep(1)
        # 输入账号
        self.drive.find_element(By.CLASS_NAME, 'ipt-tel').send_keys()
        time.sleep(1)
        # 输入密码
        self.drive.find_element(By.CLASS_NAME, 'ipt-pwd').send_keys("")
        time.sleep(1)
        # 点击登录
        self.drive.find_element(By.ID, "loginBtn").click()
        time.sleep(2)
        # 让窗口最大化 防止点不到未显示的内容
        self.drive.maximize_window()
        time.sleep(2)

    def Start(self):
        time.sleep(1)
        print("第1章:")
        a = 1
        # 开始视频循环
        while True:
            a = a+1

            sp = re.findall(
                r"<li title=\"视频\" cardid=\"\d*\" onclick=\"changeDisplayContent\(2,3,'\d*','228378902','62947598',''\);\" id=\"dct2\">",self.drive.page_source)
            # if 进行筛选分支， 这是视频在第二个列表的挂课操作
            if len(sp) != 0:
                # 点击视频列表
                self.drive.find_element(By.XPATH, "/html/body/div[4]/div/div[3]/div[4]/div/ul/li[2]/div").click()
                time.sleep(2)
                # 进入frame
                self.drive.switch_to.frame(0)
                time.sleep(2)
                # 查找有几个视频
                spsum = re.findall('<iframe src="',self.drive.page_source)
                # 进入播放循环
                for i in range(1,len(spsum)+1):
                    # 视频是在frame嵌套里，所以再进入一次frame
                    self.drive.switch_to.frame(i-1)
                    # 点击后视频开始播放
                    self.drive.find_element(By.XPATH,"/html").click()
                    # 每5秒判定视频是否播放完毕，播放完毕退出
                    while True:
                        time.sleep(5)
                        if (self.drive.find_element(By.XPATH,"/html/body/div[2]/div[4]/div/div[5]/button[1]/span[2]").text) == "重播":
                            print("该视频已刷完...")
                            break
                    # 如果不止一个视频，就退出frame，回到主页面 滑动进度条到下一个视频的位置，在进入frame
                    self.drive.switch_to.default_content()
                    js = 'window.scrollTo(0,120)'  # document.body.scrollHeight是获取滚动条的高度
                    self.drive.execute_script(js)
                    self.drive.switch_to.frame(0)


            # 当前页面就是视频所在列表或者没有视频的操作
            else:

                self.drive.switch_to.frame(0)
                time.sleep(2)
                # 查找有几个视频，或者没有视频
                spsum = re.findall('<div class="ans-job-icon " id="ext-gen1047"></div>', self.drive.page_source)
                if len(spsum) == 0:
                    print("此章节无视频")

                # 下面操作和上面一样
                else:
                    for i in range(1, len(spsum)+1):
                        self.drive.switch_to.frame(i - 1)
                        self.drive.find_element(By.XPATH, "/html").click()
                        while True:
                            time.sleep(5)
                            if (self.drive.find_element(By.XPATH,"/html/body/div[2]/div[4]/div/div[5]/button[1]/span[2]").text) == "重播":
                                print("该视频已刷完...")
                                break
                        self.drive.switch_to.default_content()
                        js = 'window.scrollTo(0,120)'  # document.body.scrollHeight是获取滚动条的高度
                        self.drive.execute_script(js)
                        self.drive.switch_to.frame(0)

            self.drive.switch_to.default_content()
            time.sleep(1)

            # 点击下一章，有可能到本章的下一个列表里了，所以看看点击下一章后，和本章节标题是否一样
            while True:
                # 本章节标题
                title0 = self.drive.find_element(By.XPATH, "/html/body/div[4]/div/div[3]/div[3]/div").text
                # 滑动进度条到下一章的位置
                js = 'window.scrollTo(0,document.body.scrollHeight)'  # document.body.scrollHeight是获取滚动条的高度
                self.drive.execute_script(js)
                # 点击下一章
                self.drive.find_element(By.XPATH, "/html/body/div[4]/div/div[3]/div[6]").click()
                time.sleep(1)
                # 点击之后的标题
                title1 = self.drive.find_element(By.XPATH, "/html/body/div[4]/div/div[3]/div[3]/div").text
                # 进行对比
                if title1 == title0:
                    continue
                else:
                    print(f"进行第{a}章:")
                    break


t1=XXT()
t1.Start()


